<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/7/5 17:49
 */

namespace App\Project\Printer;

use App\Models\PastHistory;

class PastPrinter extends PrinterAbstract
{
    use MedicationFormat;

    public function __construct(PastHistory $past)
    {
        $this->model = $past;
    }

    public function fat()
    {
        $fat = $this->model->fat;
        if ($this->isEmpty($fat)) {
            return '';
        }

        $print = [
            $this->fe('fat', 'time', '从', '起'),
            $this->fe('fat', 'weight', '体重', 'KG'),
        ];

        if (isset($fat['time']) && isset($fat['duration'])) {
            $print[] = '持续 ' . $this->diffWithFormat($fat['time'], $fat['duration']);
        }

        if (in_array("drugs", $fat)) {
            $print[] = $this->drugs($fat['drugs']);
        }
        return $this->getPrintStr('肥胖史：', $print);
    }

    public function hypertension()
    {
        if (!$this->model->hypertension) {
            return '';
        }

        $print = [
            $this->fed('hypertension', 'time', '持续'),
        ];

        if (in_array("drugs", $this->model->hypertension)) {
            $print[] = $this->drugs($this->model->hypertension['drugs']);
        }
        return $this->getPrintStr('高血压史：', $print);
    }

    public function coronaryDisease()
    {
        if (!$this->model->coronary_disease) {
            return '';
        }

        $print = [
            $this->fed('coronary_disease', 'time', '持续'),
            $this->fei('coronary_disease', 'stenting', '支架置入时间'),
            $this->fe('coronary_disease', 'cabg', '搭桥'),
        ];

        if (in_array("drugs", $this->model->coronary_disease)) {
            $print[] = $this->drugs($this->model->coronary_disease['drugs']);
        }

        return $this->getPrintStr('冠心病：', $print);
    }

    public function hyperuricemia()
    {
        if (!$this->model->hyperuricemia) {
            return '';
        }

        $print = [
            $this->fed('hyperuricemia', 'time', '持续'),
        ];

        if (in_array("drugs", $this->model->hyperuricemia)) {
            $print[] = $this->drugs($this->model->hyperuricemia['drugs']);
        }

        return $this->getPrintStr('高尿酸血症：', $print);
    }

    public function hyperlipidemia()
    {
        if (!$this->model->hyperlipidemia) {
            return '';
        }

        $print = [
            $this->fed('hyperlipidemia', 'time', '持续'),

        ];

        if (in_array("drugs", $this->model->hyperlipidemia)) {
            $print[] = $this->drugs($this->model->hyperlipidemia['drugs']);
        }

        return $this->getPrintStr('高脂血症：', $print);
    }

    public function fattyLiver()
    {
        if (!$this->model->fatty_liver) {
            return '';
        }

        $print = [
            $this->fed('fatty_liver', 'time', '持续'),
        ];

        if (in_array("drugs", $this->model->fatty_liver)) {
            $print[] = $this->drugs($this->model->fatty_liver['drugs']);
        }

        return $this->getPrintStr('脂肪肝：', $print);
    }

    public function hepatitis()
    {
        if (!$this->model->hepatitis) {
            return '';
        }

        $print = [
            $this->ft('past.hepatitis.type', $this->fe('hepatitis', 'type')),
            $this->fed('hepatitis', 'duration', '持续'),
        ];

        if (in_array("drugs", $this->model->hepatitis)) {
            $print[] = $this->drugs($this->model->hepatitis['drugs']);
        }

        return $this->getPrintStr('肝炎：', $print);
    }

    public function pancreatitis()
    {
        if (!$this->model->pancreatitis) {
            return '';
        }

        $print = [
            $this->fed('pancreatitis', 'acute', '急性'),
            $this->fed('pancreatitis', 'chronic', '慢性'),
        ];

        if (in_array("drugs", $this->model->pancreatitis)) {
            $print[] = $this->drugs($this->model->pancreatitis['drugs']);
        }

        return $this->getPrintStr('胰腺炎史：', $print);
    }

    public function pancreaticOperation()
    {
        if (!$this->model->pancreatic_operation) {
            return '';
        }

        $print = [
            $this->fe('pancreatic_operation', 'time', '手术时间 '),
            $this->fe('pancreatic_operation', 'operation', '手术名称 '),
            //TODO  张云龙修改 因为没有drugs字段
            //$this->drugs($this->model->pancreatic_operation['drugs'])
        ];

        return $this->getPrintStr('胰腺手术史：', $print);
    }

    public function renal()
    {
        if (!$this->model->renal) {
            return '';
        }

        $print = [
            $this->fed('renal', 'time', '持续'),
        ];

        if (in_array("drugs", $this->model->renal)) {
            $print[] = $this->drugs($this->model->renal['drugs']);
        }

        return $this->getPrintStr('肾功能不全：', $print);
    }

    public function cerebralInfarction()
    {
        if (!$this->model->cerebral_infarction) {
            return '';
        }

        $print = [
            $this->fed('cerebral_infarction', 'time', '持续'),
//            $this->fei('cerebral_infarction', 'drugs', '服用药物'),
        ];

        if (in_array("drugs", $this->model->cerebral_infarction)) {
            $print[] = $this->drugs($this->model->cerebral_infarction['drugs']);
        }

        return $this->getPrintStr('脑梗死：', $print);
    }

    public function pcos()
    {
        if (!$this->model->pcos) {
            return '';
        }

        $print = [
            $this->fed('pcos', 'time', '持续'),
//            $this->fei('pcos', 'drugs', '服用药物'),
        ];

        if (in_array("drugs", $this->model->pcos)) {
            $print[] = $this->drugs($this->model->pcos['drugs']);
        }

        return $this->getPrintStr('多囊卵巢综合征：', $print);
    }

    public function transfusion()
    {
        if (!$this->model->transfusion) {
            return '';
        }

        $print = [
            $this->fe('transfusion', 'times', '', '次'),
        ];

        return $this->getPrintStr('输血史：', $print);
    }

    public function irritability()
    {
        if (!$this->model->irritability) {
            return '';
        }

        $print = [
            $this->fe('irritability', 'items', '过敏物'),
        ];

        return $this->getPrintStr('过敏史：', $print);
    }

    public function specialIrritability()
    {
        if (!$this->model->special_irritability) {
            return '';
        }

        $print = [
            $this->fe('special_irritability', 'items', '使用的药物'),
        ];

        return $this->getPrintStr('特殊药物过敏史：', $print);
    }

    public function adverseDrugReaction()
    {
        if (!$this->model->adverse_drug_reaction) {
            return '';
        }

        $print = [
            $this->fe('adverse_drug_reaction', 'items', '药物'),
            $this->fe('adverse_drug_reaction', 'symptoms', '症状'),
        ];

        return $this->getPrintStr('药物不良反应：', $print);
    }

    public function thyroid()
    {
        if (!$this->model->thyroid) {
            return '';
        }

        $print = [
            $this->ft('past.thyroid.type', $this->fe('thyroid', 'type')),
            $this->fed('thyroid', 'time', '持续'),
//            $this->fei('thyroid', 'drugs', '服用药物'),
        ];

        if (in_array("drugs", $this->model->thyroid)) {
            $print[] = $this->drugs($this->model->thyroid['drugs']);
        }

        return $this->getPrintStr('甲状腺功能：', $print);
    }

    public function trauma()
    {
        if (!$this->model->trauma) {
            return '';
        }

        $print = [
            $this->fed('trauma', 'time', '持续'),
//            $this->fei('trauma', 'drugs', '服用药物：'),
        ];

        if (in_array("drugs", $this->model->trauma)) {
            $print[] = $this->drugs($this->model->trauma['drugs']);
        }

        return $this->getPrintStr('外伤手术史：', $print);
    }

    public function contagion()
    {
        if (!$this->model->contagion) {
            return '';
        }

        $print = [
            $this->fed('contagion', 'time', '持续'),
//            $this->fei('contagion', 'drugs', '服用药物：'),
        ];

        if (in_array("drugs", $this->model->contagion)) {
            $print[] = $this->drugs($this->model->contagion['drugs']);
        }

        return $this->getPrintStr('传染病史：', $print);
    }

    public function extra()
    {
        $extras = $this->model->extra;

        if (!$extras) {
            return '';
        }
        $str = '';
        foreach ($extras as $key => $extra) {
            $print = [];
            if ($this->has($extra, 'time')) {
                $print[] = '持续 ' . $this->diffWithFormat($extra['time']);
            }

            if ($this->has($extra, 'drugs')) {
//                $print[] = '服用药物：' . implode(',', $extra['drugs']);
                $print[] = $this->drugs($extra['drugs']);
            }

            if ($this->has($extra, 'remark')) {
                $print[] = '备注：' . $extra['remark'];
            }

            $str .= $this->getPrintStr($extra['name'] . '：', $print) . '<br>';
        }

        return $str;
    }

    public function other()
    {
        if (!$this->model->other) {
            return '';
        }

        $print = [
            $this->fe('other', 'time'),
        ];

        if (in_array("drugs", $this->model->other)) {
            $print[] = $this->drugs($this->model->other['drugs']);
        }


        return $this->getPrintStr('其它：', $print);
    }

    public function operation()
    {
        if (!$this->model->operation) {
            return '';
        }

        $print = [
            $this->fe('operation', 'time'),
        ];

        if (in_array("drugs", $this->model->operation)) {
            $print[] = $this->drugs($this->model->operation['drugs']);
        }

        return $this->getPrintStr('其它：', $print);
    }

    /**
     * @param $drugs
     * @return string
     */
    protected function drugs($drugs)
    {
        if (isset($drugs) && !empty($drugs)) {
            if (is_string($drugs[0])) {
                return $this->fei('fat', 'drugs', '服用药物');
            } else {
//                if (app('scene')->isWestern()) {
//                    return $this->medicationFormat($drugs);
//                } else {
//                    return $this->cnMedicationFormat($drugs);
//                }
                return '服用药物：' . $this->medicationFormat($drugs);
            }
        }

        return '';
    }
}